package com.xmh.reptile.service;

import cn.hutool.core.io.FileUtil;
import com.xmh.reptile.util.RedisMockUtil;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class NovelTMDFPService {

    public static final String key = "天命大反派shucw";
    public static final String url = "https://www.shucw.com/html/13/13889/";
    public static final String listCssQuery = "#list dl dd a";

    public static final String detailUrl = "https://www.shucw.com";
    public static final String startLine = "\t\t";

    public static long newPageNumber = Long.parseLong(String.valueOf(RedisMockUtil.get(key) == null ? 0 : RedisMockUtil.get(key)));

    public static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");

    public static void wgzz() throws InterruptedException {
        StringBuilder sb = new StringBuilder();
        try {
            Document doc = Jsoup.connect(url).get();

            Elements list = doc.select(listCssQuery);

            for (Element element : list) {
                String href = element.attr("href");

                String title = element.html();

                String pageNumber = StringUtils.substringAfterLast(href, "/").split("\\.")[0];

                if (Long.parseLong(pageNumber) <= newPageNumber) {
                    continue;
                } else {
                    newPageNumber = Long.parseLong(pageNumber);
                }

                sb.append("\n\n" + startLine + "====== ").append(title).append(" =====\n\n");

                Document detailPageDocument = Jsoup.connect(detailUrl + href).get();
                Element select = detailPageDocument.getElementById("content");

                String html = select.html();

                sb.append(startLine);
                sb.append(textHandle(html));

                for (int i = 0; i < 10; i++) {
                    if (html.contains("本章未完，点击下一页继续阅读")) {
                        Thread.sleep(1000);
                        String url = new StringBuilder(href).insert(22, "_" + (i + 2)).toString();

                        Element select2 = Jsoup.connect(detailUrl + url).get().getElementById("content");

                        html = select2.html();

                        sb.append(textHandle(html));
                    } else {
                        break;
                    }
                }
                Thread.sleep(1000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (StringUtils.isNotEmpty(sb.toString())) {
            FileUtil.appendString(sb.toString(), new File("C:\\Users\\xmh\\Desktop\\" + key + simpleDateFormat.format(new Date()) + ".txt"), "UTF-8");
            RedisMockUtil.set(key, newPageNumber);
        }
    }

    private static String textHandle(String text) {
        return text.replace("&nbsp;&nbsp;&nbsp;&nbsp;", " ")
                .replace("一秒记住", "")
                .replace("\n<br>\n<br>\n<br>\n<br>", "\n")
                .replace("\n<br>\n<br>\n<br>\n<br>\n<br>", "\n")
                .replace("\n<br>\n<br>\n<br>", "\n")
                .replace("\n<br>\n<br>", "\n")
                .replace("\n    \n   \n   \n   \n     \n    ", "\n")
                .replace("\n \n ", "\n")
                .replace("\n   \n   ", "\n")
                .replace("<br>\n", "")
                .replace("<br>", "")
                .replace("&nbsp;", "")
                .replace("<span style=\"color:#333333;font-family:宋体, &quot;font-size:22px;background-color:#FBF4EC;\">", "")
                .replace("<span style=\"color:#555555;font-family:方正启体简体, &quot;font-size:25.3333px;background-color:#E9FAFF;\">", "")
                .replace("</span>", "")
                .replace("&nbsp;", "")
                .replace("<span style=\"color:#333333;font-family:宋体, &quot;font-size:22px;background-color:#FBF4EC;\">", "")
                .replace("<span style=\"color:#555555;font-family:方正启体简体, &quot;font-size:25.3333px;background-color:#E9FAFF;\">", "")
                .replace("</span>", "")
                .replace("记住网址uxzw．com", "")
                .replace("<p>", "")
                .replace("</p>", "")
                .replace("<center>", "")
                .replace("</center>", "")
                .replace("--&gt;&gt;本章未完，点击下一页继续阅读", "")
                .replace("\n", "\n\t\t");
    }

}
